package com.seanliao.nav.controller;

import com.seanliao.nav.common.domain.dto.PermissionDTO;
import com.seanliao.nav.common.domain.dto.valid.Add;
import com.seanliao.nav.common.domain.dto.valid.Edit;
import com.seanliao.nav.common.domain.vo.PermissionVO;
import com.seanliao.nav.common.response.Result;
import com.seanliao.nav.common.response.ResultBuilder;
import com.seanliao.nav.service.ISysPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 权限Controller
 */
@RestController
@RequestMapping("/permi")
public class PermissionController {

    @Autowired
    private ISysPermissionService permissionService;

    /**
     * 获取当前用户目录权限树
     * 不显示禁用权限
     */
    @GetMapping("/menu")
    public Result<List<PermissionVO>> selectMenuForCurrentUser() {
        List<PermissionVO> permissionVOList = permissionService.selectMenuForCurrentUser();
        return ResultBuilder.success(permissionVOList);
    }

    /**
     * 获取所有目录菜单
     */
    @GetMapping("/selectMenu")
    @PreAuthorize("@pms.hasPermission('permi:select')")
    public Result<List<PermissionVO>> selectMenu() {
        List<PermissionVO> permissionVOList = permissionService.selectMenu();
        return ResultBuilder.success(permissionVOList);
    }

    /**
     * 获取菜单下权限
     * 显示禁用权限
     */
    @GetMapping("/selectPermi")
    @PreAuthorize("@pms.hasPermission('permi:select')")
    public Result<List<PermissionVO>> selectPermission(@RequestParam Long id) {
        List<PermissionVO> permissionVOList = permissionService.selectPermission(id);
        return ResultBuilder.success(permissionVOList);
    }


    /**
     * 获取指定角色权限列表
     * 不显示禁用权限
     */
    @GetMapping("/selectByRoleId")
    @PreAuthorize("@pms.hasPermission('permi:selectByRoleId')")
    public Result<List<PermissionVO>> selectByRoleId(@RequestParam Long roleId) {
        List<PermissionVO> permissionVOList = permissionService.selectByRoleId(roleId);
        return ResultBuilder.success(permissionVOList);
    }

    /**
     * 添加权限
     */
    @PostMapping("/add")
    @PreAuthorize("@pms.hasPermission('permi:add')")
    public Result<?> add(@Validated(Add.class)  @RequestBody PermissionDTO permissionDTO) {
        permissionService.add(permissionDTO);
        return ResultBuilder.success();
    }

    /**
     * 编辑权限
     */
    @PostMapping("/edit")
    @PreAuthorize("@pms.hasPermission('permi:edit')")
    public Result<?> edit(@Validated(Edit.class) @RequestBody PermissionDTO permissionDTO) {
        permissionService.edit(permissionDTO);
        return ResultBuilder.success();
    }

    /**
     * 删除权限
     */
    @PostMapping("/remove")
    @PreAuthorize("@pms.hasPermission('permi:remove')")
    public Result<?> remove(@RequestParam Long permissionId) {
        permissionService.removeById(permissionId);
        return ResultBuilder.success();
    }

    /**
     * 排序
     */
    @PostMapping("/sort")
    @PreAuthorize("@pms.hasPermission('permi:sort')")
    public Result<?> sort(@Validated(Edit.class) @RequestBody List<PermissionDTO> permissionDTOList) {
        permissionService.sort(permissionDTOList);
        return ResultBuilder.success();
    }

    /**
     * 根据角色分配权限
     */
    @PostMapping("/allocateByRole")
    @PreAuthorize("@pms.hasPermission('permi:allocate')")
    public Result<?> editRolePermission(@RequestParam Long roleId,
                                     @RequestParam List<Long> permissionIds) {
        permissionService.editRolePermission(roleId, permissionIds);
        return ResultBuilder.success();
    }

    /**
     * 根据权限分配权限
     */
    @PostMapping("/allocateByPermi")
    @PreAuthorize("@pms.hasPermission('permi:allocate')")
    public Result<?> editPermissionRole(@RequestParam Long permissionId,
                                     @RequestParam List<Long> roleIds) {
        permissionService.editPermissionRole(permissionId, roleIds);
        return ResultBuilder.success();
    }

}
